class People():
    nation = 'CN'

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def get_name(self):
        print(self.name)


if __name__ == '__main__':
    p = People('小明', 18)
    print(hasattr(People, 'nation'))
    print(hasattr(p, 'age'))
    print(hasattr(People, 'get_name'))
    print(hasattr(People, 'xxxxxx'))

    # 反射获取类变量
    print(getattr(People, 'nation'))
    # 反射获取成员变量
    print(getattr(p, 'name'))
    # 反射获取成员方法，返回方法是一个引用地址，要想执行该方法，需要在后面加上小括号
    getattr(p, 'get_name')()

    setattr(p, 'name', '小小明')
    print(getattr(p, 'name'))
    setattr(p, 'new_get_name', p.get_name)
    getattr(p, 'new_get_name')()

    delattr(p, 'name')
    getattr(p, 'name')
    delattr(p, 'new_get_name')
    getattr(p, 'new_get_name')()
